import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:loongson_block/error.dart';
import 'package:loongson_block/utils/state.dart';
import 'package:loongson_block/web.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

void main() {
  runApp(GetMaterialApp(
    home: const MyHomePage(),
    theme: ThemeData(primarySwatch: Colors.red),
    builder: EasyLoading.init(),
  ));
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final controller = TextEditingController();

  @override
  void initState() {
    controller.text = '192.168.8.240';
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final width = MediaQuery.of(context).size.width;

    return (width > 700)
        ? SafeArea(
            child: Scaffold(
            // drawer: const Drawer(),
            appBar: AppBar(
              title: const Center(child: Text('Openblock-Loongson 龙芯教育派')),
              actions: [
                InkWell(
                    onTap: () {},
                    child: const Padding(padding: EdgeInsets.symmetric(horizontal: 20), child: Icon(Icons.refresh))),
                InkWell(
                    onTap: () {},
                    child: const Padding(padding: EdgeInsets.symmetric(horizontal: 20), child: Icon(Icons.settings))),
              ],
            ),
            body: Center(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  const Text('输入龙芯教育派 IP: '),
                  SizedBox(
                    width: 160,
                    child: TextField(
                      autofocus: true,
                      style: const TextStyle(fontSize: 18),
                      controller: controller,
                      textAlign: TextAlign.center,
                      keyboardType: TextInputType.number,
                      inputFormatters: [FilteringTextInputFormatter.allow(RegExp('[.0-9]'))],
                    ),
                  ),
                  OutlinedButton(
                      onPressed: () {
                        SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);
                        ip.value = controller.text;
                        Get.to(() => const WebMain());
                      },
                      child: const Text('确认')),
                ],
              ),
            ),
          ))
        : const ErrorPage();
  }
}
